package com.item.service;

import com.api.MEnumError;
import com.base.api.ApiException;
import com.base.entity.QueryParams;
import com.base.web.annotation.LoginMethod;
import com.item.dao.MobileVerifyMapper;
import com.item.dao.UserMapper;
import com.item.dao.model.User;
import com.item.dao.model.UserExample;
import com.item.daoEx.UserMapperEx;
import com.item.daoEx.model.UserEx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserMapperEx userMapperEx;
	@Autowired
	private MobileVerifyMapper verifyMapper;
	@Autowired
	private NotifyService notifyService;

	public int countByExample(UserExample example) {
		return this.userMapper.countByExample(example);
	}

	public User selectByPrimaryKey(Integer id) {
		return this.userMapper.selectByPrimaryKey(id);
	}

	public List<User> selectByExample(UserExample example) {
		return this.userMapper.selectByExample(example);
	}

	public int deleteByPrimaryKey(Integer id) {
		return this.userMapper.deleteByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(User record) {
		return this.userMapper.updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(User record) {
		return this.userMapper.updateByPrimaryKey(record);
	}

	public int deleteByExample(UserExample example) {
		return this.userMapper.deleteByExample(example);
	}

	public int updateByExampleSelective(User record, UserExample example) {
		return this.userMapper.updateByExampleSelective(record, example);
	}

	public int updateByExample(User record, UserExample example) {
		return this.userMapper.updateByExample(record, example);
	}

	public int insert(User record) {
		int cnt = this.userMapper.insert(record);
		//notifyService.insertNewUser(record.getId());
		return cnt;
	}

	public int insertSelective(User record) {
		int cnt =  this.userMapper.insertSelective(record);
		//notifyService.insertNewUser(record.getId());
		return cnt;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public int updateStrength(QueryParams.Builder builder){
		return this.userMapperEx.updateStrength(builder);
	}

	public List<UserEx> selectList(Map<String, Object> map) {
		return userMapperEx.selectList(map);
	}
	
	public List<Integer> selectIdByAccounts(List<String> accounts){
		return userMapperEx.selectIdByAccounts(accounts);
	}
	
	public Integer selectIdByAccount(String account){
		return userMapperEx.selectIdByAccount(account);
	}
	
	public String selectAccountById(String id){
		return userMapperEx.selectAccountById(id);
	}
	
    public boolean autoH5Login(HttpServletRequest request,HttpServletResponse response){
		Cookie[] cookies = request.getCookies();
		Cookie c = null;
		String account = "";
		String pwd = "";
		for (int i = 0; cookies!=null && i < cookies.length; i++){
			c = cookies[i];
			if (c.getName().equals("account")){
				account = c.getValue();
			}else if(c.getName().equals("pwd")){
				pwd = c.getValue();
			}
		}
		if(account.length()>0 && pwd.length()>0){
			UserExample example = new UserExample();
			example.createCriteria().andAccountEqualTo(account).andPasswordEqualTo(pwd);
			List<User> list = this.selectByExample(example);
			if(list.size()>0){
				User user = list.get(0);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", user.getId());
				map.put("account", user.getAccount());
				request.getSession().setAttribute(LoginMethod.PHONE.getName(), map);
				return true;
			}
		}
    	return false;
    }

    public List<UserEx> selectNear(QueryParams.Builder builder){
    	return this.userMapperEx.selectNear(builder);
	}

	public List<UserEx> selectAppList(QueryParams.Builder builder){
    	return this.userMapperEx.selectAppList(builder);
	}

	public List<UserEx> selectMyFriend(Integer userId){
		return this.userMapperEx.selectMyFriend(userId);
	}

	public User regist(String phone, String password,String nickName) {
		return regist(phone,password,nickName,null);
	}

	public User regist(String phone, String password,String nickName,String headImg) {
		// 添加数据
		Date date = new Date();
		User record = new User();
		record.setAccount(phone);
		record.setCreateTime(date);
		record.setUserState(0);
		record.setIsVip(0);
		record.setSex(0);
		record.setUserType(1);
		record.setCredit(0);
		record.setPassword(password);
		record.setNickName(nickName);
		record.setHeadImg(headImg);
		record.setIsVerify(0);
		record.setIsBindPhone(1);
		record.setBindPhone(phone);
		// 保存
		int cnt = userMapper.insertSelective(record);
		if (cnt == 0) {
			throw new ApiException(MEnumError.CREATE_ACCOUNT_ERROR);
		}
		//64位编码
		userMapper.updateByPrimaryKey(record);

		record.setAccount(record.getAccount());

		return record;
	}

	public List<UserEx> selectWebList(QueryParams.Builder builder){
		return this.userMapperEx.selectWebList(builder);
	}

	/**
	 * 艺术家列表
	 * @param map
	 * @return
	 */
    public List<UserEx> selectArtList(Map<String, Object> map) {
    	return this.userMapperEx.selectArtList(map);
    }

	/**
	 * 用户动态详情
	 * @param map
	 * @return
	 */
	public UserEx selectNewsDetail(Map<String, Object> map) {
		return this.userMapperEx.selectNewsDetail(map);
    }

    public int updateCreditList(Map<String, Object> map) {
		return this.userMapperEx.updateCreditList(map);
    }

    public List<UserEx> selectMyFocus(QueryParams.Builder builder){
		return this.userMapperEx.selectMyFocus(builder);
	}

	public String getVip(Integer credit){
    	String vip;
		if (credit == null){
			credit = 0;
		}
		if (credit > 1000){
			vip = "VIP5";
		}else if (credit > 700){
			vip = "VIP4";
		}else if (credit > 400){
			vip = "VIP3";
		}else if (credit > 100){
			vip = "VIP2";
		}else if(credit >= 0){
			vip = "VIP1";
		}else{
			vip = "VIP0";
		}
		return vip;
	}

	public int updateUserCredit(List<User> users,Integer credit) {
		return this.userMapperEx.updateUserCredit(users,credit);
	}
}